package com.lk.mi.controller;

import com.lk.mi.entity.item;
import com.lk.mi.entity.shopCart;
import com.lk.mi.service.shopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shopCart")
public class ShopCartController {

    @Autowired
    private shopCartService shopCartService;

    /**
     * 根据用户id查询用户购物车
     * @return
     */
    @RequestMapping("/findCart")
    public String findCart(HttpSession session, Model model){
        Integer id = (Integer) session.getAttribute("SESSION_USERID");
        List<shopCart> list = shopCartService.findCart(id);
        double totalAmount = getTotalAmount(id);

        model.addAttribute("list",list);
        model.addAttribute("totalAmount",totalAmount);

        return "/pages/shopcart.jsp";
    }

    /**
     * 添加一个商品(在商城页面)
     * @param iid
     * @param session
     * @return
     */
    @RequestMapping("/addOne")
    public String addOne(Integer iid,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");
        //根据商品id查询商品信息
        item item = shopCartService.addOne(iid);
        //根据用户id和商品id查询购物车信息
        shopCart shopCart=shopCartService.findByTwo(uid,iid);
        //如果查询结果为空，设置数量为1
        if (shopCart == null){
            shopCart shopCart1 = new shopCart(0,uid,iid,item.getImage(),item.getItemName(),item.getPrice(),1,item.getPrice());
            shopCartService.add(shopCart1);
        }else {
            //如果查到结果，number++,并且在该类型商品总价上价格加一次
            shopCartService.updateByUidAndIid(uid, iid);
        }
        return "/item/findAll";
    }

    /**
     * 添加一个商品(在购物车页面)
     * @param iid
     * @param session
     * @return
     */
    @RequestMapping("/add1")
    public String add1(Integer iid,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");

        //必查到结果，number++,并且在该类型商品总价上价格加一次
        shopCartService.updateByUidAndIid(uid, iid);

        return "/shopCart/findCart";
    }

    /**
     * 减少一个商品(在购物车页面)
     * @param iid
     * @param session
     * @return
     */
    @RequestMapping("/less1")
    public String less1(Integer iid,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");

        //必查到结果,该商品数减一,总价减一次。
        shopCartService.updateByUidAndIid2(uid, iid);
        //根据用户id和商品id查询购物车信息,如果number==0，删除该条订单。
        shopCart shopCart=shopCartService.findByTwo(uid,iid);
        if (shopCart.getNumber()==0){
            shopCartService.delete(shopCart.getId());
        }
        return "/shopCart/findCart";
    }


    //计算总价钱
    public double getTotalAmount(Integer uid){
        List<shopCart> list = shopCartService.findSbAllShopCart(uid);
        //遍历,用个数乘以金额，相加
        double totalAmount=0;
        for (shopCart shopCart:list) {
            double price = shopCart.getPrice();
            Integer number = shopCart.getNumber();
            totalAmount+=price*number;
        }
        return totalAmount;
    }


}
